package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.demo.domain.Category;
import com.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags="分类详情")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页显示分类管理
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分类查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",readOnly = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",readOnly = true)
    })
    public R<Page> page(int page,int pageSize){
        log.info("前端传来的page={} pageSize={}",page,pageSize);
        return categoryService.page(page,pageSize);

    }

    /**
     * 添加菜品类型与套餐
     * @param request
     * @param category
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "添加菜品类型与套餐")
    public R<String> save (HttpServletRequest request,@RequestBody Category category){
        log.info("前端传来的数据为："+category.toString());
        return categoryService.save(request,category);
    }

    /**
     * 删除菜品类型与套餐
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除菜品类型与套餐")
    public R<String> deleteById(Long id){
        log.info("需要删除的id为: {}" ,id);
        return categoryService.deleteById(id);
    }

    /**
     * 修改菜品分类信息
     * @param category
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品分类信息")
    public R<Category> updateCategory(@RequestBody Category category){
        log.info("需要修改的id为：{}" ,category.getId());
        return categoryService.updateCategory(category);
    }

    /**
     * 菜品类型查询
     * @param category
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "菜品类型查询")
    public R<List<Category>> getDishList(Category category){
        log.info("菜品类型为：{}",category.getType());

        return categoryService.getDishList(category);
    }
 }
